<?php
/**
 * Created by PhpStorm.
 * User: dale047
 * Date: 2019/4/20
 * Time: 下午7:03
 */

namespace app\teacher\controller;
use think\Controller;
use think\Request;

class Common extends Controller
{
    private $config;
    private $instructor;
    private $leave;
    private $reward;
    private $night;
    private $money;
    private $student;
    private $userlogin;
    private $classinfo;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $isLogin = $this->isLogin();

        if(!$isLogin){
            $this->redirect('teacher/login/index');
        }

        $this->config     = Model('Config');
        $this->instructor = Model('Instructor');
        $this->leave      = Model('Leave');
        $this->reward     = Model('Reward');
        $this->night      = Model('Night');
        $this->money      = Model('Money');
        $this->student    = Model('Student');
        $this->userlogin  = Model('Userlogin');
        $this->classinfo  = Model('Classinfo');

        /*网站站点信息*/
        $SiteName = $this->config->getSiteName();
        /*网站版权信息*/
        $CopyName = $this->config->getCopyName();
        /*网站版权信息*/
        $SiteTel = $this->config->getSiteTel();
        /*所有请假信息处理*/
        $AllWaitLeave = $this->leave->getAllWaitLeave();
        $CountAllWaitLeave = $this->leave->countAllWaitLeave();
        $AllStuLeave = $this->leave->getAllStuLeave();
        $countAllLeave = $this->leave->countAllLeave();

        /*所有获奖信息处理*/
        $AllWaitReward = $this->reward->getAllWaitReward();
        $AllStuReward = $this->reward->getAllStuReward();
        $CountAllWaitReward = $this->reward->countAllWaitReward();
        $countAllReward = $this->reward->countAllReward();

        /*获取签到所有信息处理*/
        $AllStuNightSign = $this->night->getAllStuNightSign();
        $CountAllSign = $this->night->countAllSign();

        /*所有财产信息*/
        $AllASCMoneyInfo = $this->money->getAllASCMoneyInfo();

        /*所有学生信息*/
        $AllThisInsStudent = $this->student->getAllThisInsStudent();

        /*获取班级信息*/
        $ThisInsClassInfo = $this->classinfo->getThisInsClassInfo($isLogin['usernum']);

        /*所有登录信息*/
//        $this->userlogin->

        $Instructor = $this->getThisLoginIns();
        $this->assign([
            'SiteName'           =>  $SiteName,
            'CopyName'           =>  $CopyName,
            'SiteTel'            =>  $SiteTel,
            'SInstructor'        =>  $isLogin,
            'Instructor'         =>  $Instructor,
            'AllWaitLeave'       => $AllWaitLeave,
            'CountAllWaitLeave'  =>$CountAllWaitLeave,
            'AllWaitReward'      =>  $AllWaitReward,
            'CountAllWaitReward' => $CountAllWaitReward,
            'countAllLeave'      => $countAllLeave,
            'AllStuLeave'        => $AllStuLeave,
            'AllStuReward'       => $AllStuReward,
            'countAllReward'     => $countAllReward,
            'AllStuNightSign'    => $AllStuNightSign,
            'CountAllSign'       => $CountAllSign,
            'AllASCMoneyInfo'    => $AllASCMoneyInfo,
            'AllThisInsStudent'  => $AllThisInsStudent,
            'ThisInsClassInfo'   =>  $ThisInsClassInfo
        ]);
    }

    public function isLogin()
    {
        $ins_info = session(config('login.session_teacher'),'',config('login.session_teacher_scope'));
        if(!$ins_info){
            return false;
        }
        return $ins_info;
    }

    public function getIns($ins_num)
    {
        $Instructor = $this->instructor->getInstructor($ins_num);
        return $Instructor;
    }

    public function online()
    {
        $ins_num = Request::instance()->param('usernum');
        $ins_status = Request::instance()->param('status');
        $res = $this->instructor->save(['status'=>$ins_status],['usernum'=>$ins_num]);
        if(!$res){
            $this->error('参数错误');
        }
        $this->redirect('teacher/index/index');
        //echo "<script type='text/javascript'>alert('切换成功!');history.back();</script>";
    }

    public function getThisLoginIns()
    {
        $ins_info = $this->isLogin();
        $ThisLoginIns = $this->getIns($ins_info['usernum']);
        return $ThisLoginIns;
    }
}